#include "head.h"



int main(){
    char *ip = "192.168.222.130";
    char *port = "8080";

    int socket_fd = socket(AF_INET, SOCK_STREAM, 0);
    ERROR_CHECK(socket_fd, -1, "socket");
    struct sockaddr_in sockaddr;
    sockaddr.sin_family = AF_INET;
    sockaddr.sin_port = htons(atoi(port));
    sockaddr.sin_addr.s_addr = inet_addr(ip);

    int res_conn = connect(socket_fd,(struct sockaddr *)&sockaddr,sizeof(sockaddr));
    ERROR_CHECK(res_conn, -1, "connect");
    //printf("conn = %d\n", res_conn);
    int connect_flag = 1;
    cur_info *info = (cur_info *)calloc(1, sizeof(cur_info));
    strcat(info->cur_path, "/");

    // 登陆到服务器 -> send 0
    int type = 0; // 登陆操作
    send(socket_fd, &type, sizeof(int), MSG_NOSIGNAL);
    login(socket_fd);
    recv(socket_fd, info->token, sizeof(info->token), 0);
    printf("token = %s\n", info->token);

    fd_set set;
    char cmd[1024] = {0}; // 存储命令
    char cmd_backup[1024] = {0}; // 命令备份
    while(1){
        FD_ZERO(&set);
        FD_SET(STDIN_FILENO, &set);
        if(connect_flag == 1){
            FD_SET(socket_fd, &set);
        }

        select(10, &set, NULL, NULL, NULL);
        if(connect_flag == 1 && FD_ISSET(socket_fd, &set)){
            // 接收对端发送过来的数据
            char buf[100] = {0};
            int res_recv = recv(socket_fd, buf, sizeof(buf), 0);
            if (res_recv == 0){
                // 对端关闭
                FD_CLR(socket_fd, &set);
                close(socket_fd);
                connect_flag = 0;
                printf("无操作，已挂起\n");
                continue;
            }
        }
        //客户输入指令
        if(FD_ISSET(STDIN_FILENO, &set)){
            if(connect_flag == 0){
            char temp[100] = { 0 };
            read(STDIN_FILENO, temp, sizeof(temp)); 
                socket_fd = socket(AF_INET, SOCK_STREAM, 0);
                ERROR_CHECK(socket_fd, -1, "socket");
                struct sockaddr_in sockaddr;
                sockaddr.sin_family = AF_INET;
                sockaddr.sin_port = htons(atoi(port));
                sockaddr.sin_addr.s_addr = inet_addr(ip);

                int res_conn = connect(socket_fd,(struct sockaddr *)&sockaddr,sizeof(sockaddr));
                ERROR_CHECK(res_conn, -1, "connect");
                //printf("conn = %d\n", res_conn);
                connect_flag = 1;
                send(socket_fd, &connect_flag, sizeof(int), 0);
                usleep(1000);
                send(socket_fd, info->token, strlen(info->token), 0);
                continue;

            }
            // 清空cmd数组，避免多条命令之前产生干扰
            bzero(cmd, sizeof(cmd));
            // 标准输入读命令
            read(STDIN_FILENO, cmd, sizeof(cmd)); 
            // 切掉最后的换行符，拿到纯命令
            strtok(cmd, "\n"); 
            //printf("cmd = %s\n", cmd); // 末尾没有换行符
            memcpy(cmd_backup, cmd, sizeof(cmd_backup)); // 备份命令
            //printf("cmd backup = %s\n", cmd_backup);
            // 分割命令头和命令体
            char *cmd_head = strtok(cmd, " ");
            char *cmd_body = strtok(NULL, "\n");

            COMMAND command;
            bzero(&command, sizeof(command));
            // 命令处理
            cmd_process(cmd_head, cmd_body, &command);
            //printf("cmdenum = %d\n", command);
            //printf("cmd_head = %s, %ld\n", cmd_head, strlen(cmd_head));

            if (command > 0 && command < 7) { // 0-6短命令
                //printf("这是短命令\n");

                type = 3; // 登陆操作
                send(socket_fd, &type, sizeof(int), MSG_NOSIGNAL);
                // 短命令逻辑 -> send 3
                short_cmd(socket_fd, cmd_head, cmd_body, cmd_backup, info->cur_path);
                printf("cur_path:%s\n", info->cur_path);
            }
            else if(command == 7){
                bzero(info->cmd, sizeof(info->cmd));
                strcpy(info->cmd, cmd_backup);
                // 创建子线程处理文件的上传
                pthread_t tid;
                pthread_create(&tid, NULL, file_transfer, (void *)info); // 子线程开始工作
                //printf("pthread create\n");
                int res_join = pthread_join(tid, NULL);
                THREAD_ERROR_CHECK(res_join, "pthread join");
                //printf("pthread join\n");
            }
            else if(command == 8){
                bzero(info->cmd, sizeof(info->cmd));
                strcpy(info->cmd, cmd_backup); 
                // 创建子线程处理文件的下载
                pthread_t tid;
                pthread_create(&tid, NULL, file_transfer, (void *)info); // 子线程开始工作
                //printf("pthread create\n");
                int res_join = pthread_join(tid, NULL);
                THREAD_ERROR_CHECK(res_join, "pthread join");
                //printf("pthread join\n");
            }
            //printf("----------\n");
        }
    }
    close(socket_fd);
    return 0;
}
